//
//  LotteryTableViewCell.m
//  NewProject
//
//  Created by apple on 2018/5/31.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "LotteryTableViewCell.h"
#import "HomePageManager.h"

@implementation LotteryTableViewCell{
    NSDictionary* showDic;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    self.firstButton.layer.borderColor = [UIColor lightGrayColor].CGColor;
    self.secondButton.layer.borderColor = [UIColor lightGrayColor].CGColor;
    self.thirdButton.layer.borderColor = [UIColor lightGrayColor].CGColor;
    self.fourthButton.layer.borderColor = [UIColor lightGrayColor].CGColor;
}

-(void)loadCellWith:(NSDictionary*)dic{
    showDic = dic;

    self.dateLabel.text = [NSString stringWithFormat:@"%@期",showDic[@"IssueName"]];
    self.titleLabel.text = showDic[@"LotteryName"];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self loadNumberView];
    });
}

-(void)loadNumberView{
    for (UIView* view in self.numberBoxView.subviews) {
        [view removeFromSuperview];
    }
    NSString* winNumber = showDic[@"WinNumber"];
    CGFloat spacing = 6;
    CGFloat ballWidth = self.numberBoxView.frame.size.width/7-spacing;
    
    CGFloat spacingX = 0;
    NSArray* winNumberArray = [winNumber componentsSeparatedByString:@"+"];
    if ([winNumberArray count]>0) {
        NSArray* redArray;
        NSArray* blueArray;
        
        NSString* redString = [winNumberArray objectAtIndex:0];
        if ([redString rangeOfString:@","].location == NSNotFound) {
            redArray = [redString componentsSeparatedByString:@" "];
        }else{
            redArray = [redString componentsSeparatedByString:@","];
        }
        if ([winNumberArray count]>1) {
            NSString* blueString = [winNumberArray objectAtIndex:1];
            if ([blueString rangeOfString:@","].location == NSNotFound) {
                blueArray = [blueString componentsSeparatedByString:@" "];
            }else{
                blueArray = [blueString componentsSeparatedByString:@","];
            }
        }
        
        for (int i=0; i<[redArray count]; i++) {
            UIView* numberView = [[UIView alloc] initWithFrame:CGRectMake(i*(ballWidth+spacing), 0, ballWidth, ballWidth)];
            numberView.backgroundColor = [UIColor colorWithRed:(255.0/255.0) green:(90/255.0) blue:0 alpha:1];
            UILabel* numberLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, ballWidth, ballWidth)];
            numberLabel.font = [UIFont boldSystemFontOfSize:15];
            numberLabel.textColor = [UIColor whiteColor];
            numberLabel.textAlignment = NSTextAlignmentCenter;
            numberLabel.text = [redArray objectAtIndex:i];
            [numberView addSubview:numberLabel];
            numberView.layer.cornerRadius = ballWidth*0.5;
            [self.numberBoxView addSubview:numberView];
            spacingX+=(ballWidth+spacing);
        }
        for (int i=0; i<[blueArray count]; i++) {
            UIView* numberView = [[UIView alloc] initWithFrame:CGRectMake(spacingX, 0, ballWidth, ballWidth)];
            numberView.backgroundColor = [UIColor colorWithRed:(68.0/255.0) green:(160/255.0) blue:(255.0/255.0) alpha:1];
            UILabel* numberLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, ballWidth, ballWidth)];
            numberLabel.font = [UIFont boldSystemFontOfSize:15];
            numberLabel.textColor = [UIColor whiteColor];
            numberLabel.textAlignment = NSTextAlignmentCenter;
            numberLabel.text = [blueArray objectAtIndex:i];
            [numberView addSubview:numberLabel];
            numberView.layer.cornerRadius = ballWidth*0.5;
            [self.numberBoxView addSubview:numberView];
            spacingX+=(ballWidth+spacing);
        }
        
    }
    
}

- (IBAction)buttonAction:(UIButton *)sender {
    [LotteryManager sharedManager].currentLotteryID = showDic[@"LotteryID"];
    [LotteryManager sharedManager].currentLotteryName = showDic[@"LotteryName"];
    NSInteger actionIndex = 0;
    switch (sender.tag) {
        case 101:
            actionIndex = 1;
            break;
        case 102:
            actionIndex = 2;
            break;
        case 103:
            actionIndex = 3;
            break;
        case 104:
            actionIndex = 4;
            break;
        default:
            break;
    }
    [HomePageManager sharedManager].actionIndex = actionIndex;
    [[NSNotificationCenter defaultCenter] postNotificationName:@"kNextAction" object:nil];
}



- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end
